/*
 * Copyright (c) 2021 Huawei Device Co.,  Ltd.
 * Licensed under the Apache License,  Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,  software
 * distributed under the License is distributed on an "AS IS" BASIS, 
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,  either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include "recptprinter_observer_proxy.h"
#include <string.h>
#include <ipc_types.h>
#include <message_option.h>
#include <message_parcel.h>

#include "recptprinter_hilog.h"
#include "recptprinter_errors.h"
#include "recptprinter_common.h"

namespace SwanLink {
namespace Finance {


// 调用NAPI 的接口
void RecptprinterObserverProxy::OnSuccess(int num, int total) {
    sptr<IRemoteObject> remote = Remote();
    if (remote == nullptr) return;
    MessageParcel data;
    MessageParcel reply;
    MessageOption option;
    
    RECPTPRINTER_HILOGD(RECPTPRINTER_SRV,  "OnSuccess");
    if (!data.WriteInterfaceToken(RecptprinterObserverProxy::GetDescriptor())) {
        RECPTPRINTER_HILOGE(RECPTPRINTER_SRV,  "Write descriptor failed");
        return;
    }
    
    WRITE_PARCEL_NO_RET(data, Int32, num);
    WRITE_PARCEL_NO_RET(data, Int32, total);
    
    int ret = remote->SendRequest(RECPTPRINTER_OBSERVER_ID_ONSUCCESS,  data,  reply,  option);

    if (ret != ERR_OK) {
        RECPTPRINTER_HILOGE(RECPTPRINTER_SRV,  "SendRequest is failed,  ret: %d",  ret);
        return;
    }
}
void RecptprinterObserverProxy::OnFailed(int code, const std::string& err) {
    sptr<IRemoteObject> remote = Remote();
    if (remote == nullptr) return;
    MessageParcel data;
    MessageParcel reply;
    MessageOption option;
    
    RECPTPRINTER_HILOGD(RECPTPRINTER_SRV,  "OnFailed");
    if (!data.WriteInterfaceToken(RecptprinterObserverProxy::GetDescriptor())) {
        RECPTPRINTER_HILOGE(RECPTPRINTER_SRV,  "Write descriptor failed");
        return;
    }
    
    WRITE_PARCEL_NO_RET(data, Int32, code);
    WRITE_PARCEL_NO_RET(data, String, err);
    
    int ret = remote->SendRequest(RECPTPRINTER_OBSERVER_ID_ONFAILED,  data,  reply,  option);

    if (ret != ERR_OK) {
        RECPTPRINTER_HILOGE(RECPTPRINTER_SRV,  "SendRequest is failed,  ret: %d",  ret);
        return;
    }
}
}  // namespace Finance
}  // namespace SwanLink
